<?
session_start();
include( 'dbconnect.php' );

function save_tmp( $POSTDATA , $UserId ){
	
	if( $_POST[ 'project_id' ] == "" ){
		// non-existing project: cr4eate one:
			$sql = "insert into project( user_id ) values( '".$_SESSION[ 'user' ][ 'id' ]."')";
			mysql_query( $sql );
			$_POST[ 'project_id' ] = mysql_insert_id();
		}
		$project_id = $_POST[ 'project_id' ];
		unset( $_POST[ 'project_id' ] );
		
		$sql = "update project set project_id = '".$project_id."'";
		foreach( $POSTDATA as $k => $v ){
				$sql .= ",". $k ." = '".$v."' ";
		}
		$sql .= " where project_id='".$project_id."'";
		mysql_query( $sql );

		//echo $sql."<br />".mysql_error()."<hr>";exit();

		
		return( $project_id );
		
	
	
	
	



}


//echo "<pre >";print_r( $_POST );echo "</pre>";exit();
if( isset( $_POST[ 'f_name' ] ) )
switch( $_POST[ 'f_name' ] ){

	case "project":
	///// save all in table project = project_tmp ( 24 gio max)
	unset( $_POST[ 'f_name' ] );
	
	$redir = $_POST[ 'redir' ];
	unset( $_POST[ 'redir' ] );
	
	//// save project in temp database
	$project_id = save_tmp( $_POST , $_SESSION[ 'user' ][ 'id' ] );
				//save all POST var in session vars:
	$proj = "-".$project_id;
		//$_SESSION[ 'project' ][ 1 ] = $project_id;
	switch( $redir ){
	case "preview":$redir .= $proj;break;
	}
		/*
		$_SESSION[ 'login' ][ 'new_user' ] = 1;
			header( 'location:login' );
		*/
		
		//// then ask for login or registration:
	header( 'location:'.$redir );
	break;
		
		}

		
		
		
		
		


?>